package com.xgj.ph.dao.impl;

import com.xgj.ph.dao.ShopDao;
import com.xgj.ph.entity.Shop;
import com.xgj.ph.util.JDBCDruidUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public class ShopDaoImpl implements ShopDao {
    @Override
    public int insert(Shop shop) {
        String sql = "insert into shop values(null,?,?,?,?,?,?,?,?,?,?,?,null)";
        Object obj[] = {
                shop.getShopName(),
                shop.getShopPhone(),
                shop.getShopAddress(),
                shop.getShopDescribe(),
                shop.getShopStatus(),
                shop.getShopProvince(),
                shop.getShopCity(),
                shop.getShopDistrict(),
                shop.getShopLatitude(),
                shop.getShopLongitude(),
                shop.getAddTime()
        };
        return JDBCDruidUtil.update(sql,obj);
    }

    @Override
    public int update(Shop shop) {
        String sql = "update shop set shopName=?,shopPhone=?,shopAddress=?,shopDescribe=?,shopProvince=?,shopCity=?,shopDistrict=?,shopLatitude=?,shopLongitude=? where shopId=?";
        Object obj[]={
                shop.getShopName(),
                shop.getShopPhone(),
                shop.getShopAddress(),
                shop.getShopDescribe(),
                shop.getShopProvince(),
                shop.getShopCity(),
                shop.getShopDistrict(),
                shop.getShopLatitude(),
                shop.getShopLongitude(),
                shop.getShopId()
        };
        return JDBCDruidUtil.update(sql,obj);
    }

    @Override
    public int updateShop(int shopId,int communityId) {
        String sql = "update shop set shopCommunityId = null where shopId = ?";
        Object obj[] = {shopId};
        this.updateShopCount(communityId);
        return JDBCDruidUtil.update(sql,obj);
    }

    @Override
    public int updateShopCount(int communityId) {

        String sql = "update community set communityShopCount=(select count(*) from shop where shopCommunityId = ?) where communityId=?";
        Object obj[] = {communityId,communityId};
        System.out.println("减1关联门店 id为："+communityId);
        return JDBCDruidUtil.update(sql,obj);
    }

    @Override
    public int delete(int shopId) {
        String sql = "delete from shop where shopId=?";
        Object obj[] = {shopId};
        return JDBCDruidUtil.update(sql,obj);
    }

    //  门店查询功能
    @Override
    public List<Shop> selectAll(Map<String, String> map) {
        List<Shop> list = new ArrayList<>();
        //  分页查询    模糊查询（店名和电话）    时间范围查询      --动态sql
        //  创建初始sql语句   where 1=1  用来处理后面的and的情况
        String sql = "select * from shop where 1=1";
        //  把要放入sql语句中的参数设置为list集合
        List<Object> params = new ArrayList<>();
        //  判断是否是某种查询
        String shopName = map.get("shopName");
//        System.out.println(shopName);
        if (shopName != null && !"".equals(shopName)) { //如果非空
            sql+=" and shopName like ?";
            params.add("%"+shopName+"%");
        }
        String shopPhone = map.get("shopPhone");
//        System.out.println(shopPhone);
        if (shopPhone != null && !"".equals(shopPhone)) {
            sql+=" and shopPhone like ?";
            params.add("%"+shopPhone+"%");
        }
        String startTime = map.get("startTime");
        if (startTime != null && !"".equals(startTime)) {
            sql+=" and addTime between ? and ?";
            params.add(startTime);
            params.add(map.get("endTime"));
        }
        //  关联门店查询
        String communityId = map.get("communityId");
        System.out.println(communityId);
        if (communityId != null && !"".equals(communityId)) {
            sql+=" and shopCommunityId = ? ";
            params.add(communityId);
        }else{
            sql+=" and shopCommunityId is null ";
        }
        //  降序排序
        sql+=" order by shopId desc,shopCommunityId desc ";
        String pn = map.get("pageNum");
        if (pn != null && !"".equals(pn)) {
            sql+=" limit ?,? ";
            int pageNum = Integer.parseInt(pn);
            int pageSize = Integer.parseInt(map.get("pageSize"));
            params.add((pageNum - 1) * pageSize);
            params.add(pageSize);
        }

        //  查询操作
        //  1.保存的参数集合转为数组，用来方便进行数据填充
        Object[] obj = params.toArray();
        ResultSet res = JDBCDruidUtil.query(sql, obj);
        while(true){
            try {
                if(!res.next())break;
                Shop shop = new Shop(
                        res.getInt(1),
                        res.getString(2),
                        res.getString(3),
                        res.getString(4),
                        res.getString(5),
                        res.getInt(6),
                        res.getString(7),
                        res.getString(8),
                        res.getString(9),
                        res.getString(10),
                        res.getString(11),
                        res.getDate(12),
                        res.getInt(13)
                );
                list.add(shop);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        JDBCDruidUtil.close();
        return list;
    }

    @Override
    public int selectCount(Map<String, String> map) {
        List<String> params = new ArrayList<>();
        String sql = "select count(1) from shop where 1=1";
        String communityId = map.get("communityId");
        if(communityId!=null && !"".equals(communityId)){
            sql += " and shopCommunityId = ? ";
            params.add(communityId);
        }else{
            sql += " and shopCommunityId is null ";
        }
        Object obj[] = params.toArray();
        int count = 0;
        ResultSet res = JDBCDruidUtil.query(sql, obj);
        try {
            if (res.next()) {
                count = res.getInt(1);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        JDBCDruidUtil.close();
        return count;
    }

}
